package com.softserveinc.fragments.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;

import com.softserveinc.R;
import com.softserveinc.model.Movie;

public class MovieTableGridItem extends FrameLayout {

	protected static final String TAG = MovieTableGridItem.class
			.getSimpleName();

	private static final int NORMALIZE_VALUE = 2;
	private static final String N_A = "n/a";
	private static final String DATE_FORMAT = "dd-MM-yyyy";

	private Movie movie;

	private View view;

	public MovieTableGridItem(final Context context, final AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public MovieTableGridItem(final Context context, final AttributeSet attrs,
			final int defStyle) {
		super(context, attrs, defStyle);

		view = View.inflate(context, R.layout.movie_row_grid, null);

		addView(view);
	}

	public void setMovie(final Movie movie, final boolean recyclePoster) {
		this.movie = movie;

		updateView(movie, recyclePoster);
	}

	private void updateView(final Movie movie, final boolean recyclePoster) {
		setTitle(movie);

		setPoster(movie, recyclePoster);

		setRealeseDate(movie);

		setRatingBar(movie);
	}

	private void setTitle(final Movie movie2) {
		TextView title = (TextView) view
				.findViewById(R.id.value_movie_title_in_row);

		title.setText(movie.toString());
	}

	private void setPoster(final Movie movie, final boolean recyclePoster) {
		ImageView poster = (ImageView) view.findViewById(R.id.icon_poster);

		ProgressBar progressBar = (ProgressBar) view
				.findViewById(R.id.icon_poster_progress_bar);

		if (movie.getPoster() != null && !movie.getPoster().isRecycled()) {
			if (recyclePoster) {
				poster.setVisibility(View.GONE);
				movie.getPoster().recycle();
				progressBar.setVisibility(View.VISIBLE);
			} else {
				poster.setVisibility(View.VISIBLE);
				poster.setImageBitmap(movie.getPoster());
				progressBar.setVisibility(View.GONE);
			}

		} else {
			poster.setVisibility(View.GONE);
			progressBar.setVisibility(View.VISIBLE);
		}
	}

	// private void setCertification(final Movie movie) {
	// ImageView certification = (ImageView) view
	// .findViewById(R.id.icon_restriction);
	// certification.setImageResource(movie.getCertification()
	// .getValidIconID());
	// }

	private void setRatingBar(final Movie movie) {
		RatingBar ratingBar = (RatingBar) view
				.findViewById(R.id.rating_bar_movie);
		Double popularity = movie.getPopularity();

		if (popularity == null) {
			popularity = 0.;
		}

		ratingBar.setRating(popularity.floatValue() / NORMALIZE_VALUE);
		ratingBar.setFocusable(false);
		ratingBar.setEnabled(false);
	}

	private void setRealeseDate(final Movie movie) {
		TextView releaseDateText = (TextView) view
				.findViewById(R.id.value_release_date_in_row);

		SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
		Date releaseDate = movie.getReleaseDate();
		if (releaseDate == null || releaseDate.getYear() == 1900) {
			releaseDateText.setText(N_A);
		} else {
			releaseDateText.setText(format.format(releaseDate));
		}
	}

	public Movie getMovie() {
		return movie;
	}
}
